package eteam.aps.gateway.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.stream.Collectors;

@Configuration
public class CommonConfig {
    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }

    /*
    public class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
        GateWayMappingJackson2HttpMessageConverter() {
            List<MediaType> mediaTypes = new ArrayList<>();
            mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
            mediaTypes.add(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
            setSupportedMediaTypes(mediaTypes);
        }
    }
    */

    /*
    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        //防止feign远程调用丢失请求头数据
        return new RequestInterceptor() {
            @SneakyThrows
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (requestAttributes != null) {
                    HttpServletRequest request = requestAttributes.getRequest();
                    request.setCharacterEncoding("utf-8");
                    String userInfo = request.getHeader(name:"user");
                    if (StrUtil.isNotBlank(userInfo)) {
                        // 同步请求
                        requestTemplate.header(name:"user", userInfo);
                    }
                }
            }
        };
    }
    */

    /*
    @Bean
    public RequestInterceptor requestInterceptor() {
        RequestInterceptor requestInterceptor = (requestTemplate) -> {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (requestAttributes != null) {
                try {
                    HttpServletRequest request = requestAttributes.getRequest();
                    Enumeration<String> headerNames = request.getHeaderNames();
                    if (headerNames != null) {
                        while (headerNames.hasMoreElements()) {
                            String name = headerNames.nextElement();
                            String values = request.getHeader(name);
                            requestTemplate.header(name, values);
                        }
                    }
                    StringBuffer body = new StringBuffer();
                    Enumeration<String> bodyNames = request.getParameterNames();
                    if (bodyNames != null) {
                        Map map = new HashMap();
                        while (bodyNames.hasMoreElements()) {
                            String name = bodyNames.nextElement();
                            String values = request.getParameter(name);
                            requestTemplate.query(name, values);
                            map.put(name, values);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        return requestInterceptor;
    }
    */
}
